<?php
/**
 * 微信用户
 * 跃飞科技版权所有 @2017
 * User: 钟贵廷
 * Date: 2017/5/19
 * Time: 14:15
 */

namespace app\admin\model;

use app\admin\controller\Admin;
use think\Model;

class User extends Model
{
    protected $resultSetType = 'collection';

    //关注公众号添加用户
    public function add($user)
    {
        $user = (array)$user;
        $this->username = $user['FromUserName'];
        $this->add_time = $user['CreateTime'];
        $admin = new Admin();
        $access_token = $admin->get_access_token();
        $userInfo = $this->info($access_token, $user['FromUserName']);
        $this->nickname = $userInfo['nickname'];
        $this->sex = $userInfo['sex'];
        $this->city = $userInfo['city'];
        $this->province = $userInfo['province'];
        $this->country = $userInfo['country'];
        $this->headimgurl = $userInfo['headimgurl'];
        $this->save();
    }

    //获取用户信息
    public function info($access_token,$openid)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN";
        $res = https_request($url);
        return $res;
    }
}